package de.tslarusso.flashcache
{
	import de.tslarusso.flashcache.cache.CacheHash;
	import de.tslarusso.flashcache.cache.CacheItem;
	import de.tslarusso.flashcache.events.CacheItemEvent;
	import de.tslarusso.larusso_stdlib.data.HashMap;
	
	public class Cache extends HashMap
	{
		private static var _instance:Cache;               
 				 		
		public function Cache(se:SingletonEnforcer)
		{
			super();
		}
		
		public static function getInstance():Cache 
		{ 
     		if(_instance == null) 
     		{ 
        		_instance = new Cache( new SingletonEnforcer() ); 
     		} 
        	return _instance; 
      	}
      	      	      	
      	public function addCacheItem( item:Object , refId:String , timeToLife:Number = 100000 ):void
      	{
      		if( !this.containsKey( refId ) )
      		{
	      		var cacheItem:CacheItem = new CacheItem();
	      		cacheItem.addData( item , refId , timeToLife );
				cacheItem.addEventListener( CacheItemEvent.ON_EXPIRED , onItemExpiredHandler , false, 0 , true );
				
				this.addItem( refId , item );
      		}
      	}
      	
      	protected function onItemExpiredHandler( e:CacheItemEvent ):void
      	{
      		this.removeItem( e.cacheItem.hash );
      	}
      	
      	public function removeCacheItem():void
      	{
      		
      	}
      	
      	public function checkCache():Boolean
      	{
      		return false;
      	}
      	
      	public function clearCache():void
      	{
      		
      	}
      	      	
      	override public function toArray( asReference:Boolean = false ):Array
      	{
      		return super.toArray( asReference );
      	}

	}
	
}
class SingletonEnforcer {}